package com.af.common;

import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

public class CommonUtil {

	@SuppressWarnings("unchecked")
	/**
	 * 新封装访问请求参数
	 */
	public Map returnParamMap(HttpServletRequest request) {
		Map map = request.getParameterMap();
		Map newMap = new HashMap();
		Iterator iterator = map.entrySet().iterator();
		while (iterator.hasNext()) {
			Map.Entry me = (Map.Entry) iterator.next();
			String key = me.getKey().toString();
			String[] value = (String[]) me.getValue();
			try {
				if(CommonUtil.isNotNullOrEmpty(map.get("RateData"))){
				newMap.put(key, new String(value[0].getBytes("ISO-8859-1"),"UTF-8"));
				}else{
					newMap.put(key,value[0]);	
				}
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return newMap;
	}

	/**
	 *对象是否为空或空字符串 
	 * @param aValue
	 * @return
	 */
	public static boolean isNotNullOrEmpty(Object aValue) {
		boolean lRtrnVal = true;
		if ((aValue == null) || (aValue.equals("")) || (aValue.equals("null"))) {
			lRtrnVal = false;
		}
		return lRtrnVal;
	}

	/**
	 * 返回格式化时间
	 * @param format
	 * @return
	 */
	public static String returnDateFormat(String format) {
		return new SimpleDateFormat(format).format(new Date());
	}
	
	public static String returnParamsNum(int num){
		String str="";
		
		for (int i = 0; i < num; i++) {

			str+="?";
			
			if(i!=num-1)
				str +=",";
			
		}
		
		return str;
	}
	
	public static String returnShortDateTime(String dateStr,String format){
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		Date date=null;
		try {
			date = new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(dateStr);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return sdf.format(date);
	}
	
	public static String returnDateToString(Date dateTime){
		
		return new SimpleDateFormat("yyyy-MM-dd HH:mm:dd").format(dateTime);
		
	}
	
}
